extends Spatial

class_name Actor
enum State{
	idle,
	goToPos,
	pointMove,
	dj,
	paoHuoChe
}

var _skinId=1
var isDJ=false
onready var _bigScaler=$bigScaler
onready var _scaler=$bigScaler/scaler
onready var _skin=$bigScaler/scaler/skin
onready var _labelName=$nameBoard/Viewport/label_name
onready var _dialogue=$dialoguePlace/dialogue
var player
var state=State.idle setget setState
func _ready():
	_updateSkin()
	_skin.flip_h=randf()<0.5
	pass # Replace with function body.


func _process(delta):
	_updateDialogue(delta)
	_updateRotating(delta)
	if state==State.goToPos:
		_updateGoToPos(delta)
	elif state==State.pointMove:
		_updatePointMove(delta)
	elif state==State.paoHuoChe:
		_updatePaoHuoChe(delta)

func _updateSkin():
	var data=Sheets.actorSkinDic.get(_skinId,null)
	if !data:
		return
	_skin.frames=Sheets.getActorSkinSprite(_skinId)
	_skin.translation.y=data.y
	_scaler.scale=Vector3(data.scale,data.scale,data.scale)
	pass
func randomSkin():
	_skinId=int(rand_range(1,11))
	_updateSkin()
	pass
func setSkin(id):
	_skinId=id
	_updateSkin()
	pass
func _nextSkin():
	_skinId+=1
	if _skinId>9:
		_skinId=1
	_updateSkin()

func setName(uName):
	_labelName.text=uName
	pass
func setRandomPos():
	var danceArea=G.danceArea as DanceArea
	var pos=danceArea.getRandomPos()
	translation=pos
	pass

func startDJ():
	self.state=State.dj
	translation=G.djPos.translation
	scale=Vector3(1.5,1.5,1.5)
	self.state=State.idle

func stopDJ():
	setRandomPos()
	scale=Vector3(1,1,1)
	randomNormalState()
func getCenter():
	return translation+Vector3(0,0.6,0)




var _dialogTimer=0
const DIALOGE_TIME=5
func showDialogue(txt):
	_dialogue.setText(txt)
	_dialogue.visible=true
	_dialogTimer=DIALOGE_TIME
	pass
func _updateDialogue(delta):
	if _dialogTimer>0:
		_dialogTimer-=delta
		if _dialogTimer<=0:
			_hideDialogue()
func _hideDialogue():
	_dialogue.visible=false


func goLeft():
	if state==State.dj:
		return
	var area=G.danceArea as DanceArea
	var grid=area.posToGrid(translation)
	for i in range(3):
		if area.hasGrid(grid.x-1,grid.y):
			grid.x-=1
		else:
			break;
	_gotoPos=area.gridRandomPos(grid)
	self.state=State.goToPos

func goUp():
	if state==State.dj:
		return
	var area=G.danceArea as DanceArea
	var grid=area.posToGrid(translation)
	for i in range(3):
		if area.hasGrid(grid.x,grid.y-1):
			grid.y-=1
		else:
			break;
	_gotoPos=area.gridRandomPos(grid)
	self.state=State.goToPos
	pass
func goRight():
	if state==State.dj:
		return
	var area=G.danceArea as DanceArea
	var grid=area.posToGrid(translation)
	for i in range(3):
		if area.hasGrid(grid.x+1,grid.y):
			grid.x+=1
		else:
			break;
	_gotoPos=area.gridRandomPos(grid)
	self.state=State.goToPos
func goDown():
	if state==State.dj:
		return
	var area=G.danceArea as DanceArea
	var grid=area.posToGrid(translation)
	for i in range(3):
		if area.hasGrid(grid.x,grid.y+1):
			grid.y+=1
		else:
			break;
	_gotoPos=area.gridRandomPos(grid)
	self.state=State.goToPos
func gotoCenter():
	if state==State.dj:
		return
	var x=rand_range(-2,2)
	var z=rand_range(2,4)
	_gotoPos=Vector3(x,0,z)
	self.state=State.goToPos
	pass
var _gotoPos=Vector3()
const GO_TO_SPEED=3
func _updateGoToPos(delta):
	if (_gotoPos-translation).length_squared()<=pow((GO_TO_SPEED*delta),2):
		translation=_gotoPos
		startIdle()
	else:
		translation+=(_gotoPos-translation).normalized()*GO_TO_SPEED*delta

func stopMove():
	self.state=State.idle
	pass
	


var _points=[]
var _pointIndex=0
func startRandomMove():
	var pointNum=int(rand_range(1,6))
	_points.clear()
	_pointIndex=0
	var area=G.danceArea as DanceArea
	for i in range(pointNum):
		_points.append(area.getRandomPos())
	_points.append(translation)
	self.state=State.pointMove
func _updatePointMove(delta):
	var p=_points[_pointIndex]
	if !p:
		return
	var res=_moveToPoint(p,delta)
	if res: #下一个点
		_pointIndex+=1
		if _pointIndex>=_points.size():
			_pointIndex=0
	

func _moveToPoint(point,delta):
	if (point-translation).length_squared()<=pow((GO_TO_SPEED*delta),2):
		translation=point
		return true
	else:
		translation+=(point-translation).normalized()*GO_TO_SPEED*delta
		return false
func startIdle():
	self.state=State.idle
	pass

func randomNormalState():
	var r=randf()
	if r<0.4:
		startRandomMove()
	elif r<0.7:
		startIdle()
	else:
		startPaoHuoChe()

var paoHuoChePos=Vector2()
const PAO_HUO_CHE_MAX_SPEED=10
func startPaoHuoChe():
	if state==State.dj:
		return
	self.state=State.paoHuoChe
	G.paoHuoCheCtl.join(self)
func _updatePaoHuoChe(delta):
	#_moveToPoint(paoHuoChePos,delta)
	var target:Vector3=lerp(translation,paoHuoChePos,0.2)
	if (target-translation).length_squared()>pow(PAO_HUO_CHE_MAX_SPEED*delta,2):
		target=translation+(target-translation).normalized()*PAO_HUO_CHE_MAX_SPEED*delta
	translation=target
func _onExitPaoHuoChe():
	G.paoHuoCheCtl.leave(self)
	pass

func setState(value):
	if state==value:
		return
	if state==State.paoHuoChe:
		_onExitPaoHuoChe()
	state=value

#变大变小
var big=1 setget setBig
func setBig(value):
	big=value
	big=clamp(big,0.5,2)
	_bigScaler.scale=Vector3(value,value,value)

#旋转
var isRotating=false setget setIsRotating
var _rotatingDir=-1
const ROTATING_SPEED=3
func setIsRotating(value):
	if isRotating==value:
		return
	isRotating=value
	if value:
		if _skin.flip_h:
			_skin.scale.x=-1
			_rotatingDir=1
		else:
			_skin.scale.x=1
			_rotatingDir=-1
	else:
		_skin.scale.x=1
func _updateRotating(delta):
	if !isRotating:
		return
	if _rotatingDir==-1:
		_skin.scale.x-=ROTATING_SPEED*delta
		if _skin.scale.x<=-1.2:
			_rotatingDir=1
	else:
		_skin.scale.x+=ROTATING_SPEED*delta
		if _skin.scale.x>=1.2:
			_rotatingDir=-1
	_skin.flip_h=_skin.scale.x<0
